﻿using System.Text;
using Microsoft.AspNetCore.Hosting;
using NLog;
using NLog.LayoutRenderers; 

namespace MyCommon.NLog
{
    [LayoutRenderer("userid")]
    public class NLogUserId : LayoutRenderer
    {
        protected override void Append(StringBuilder builder, LogEventInfo logEvent)
        {
            builder.Append("123");
        }
    }

    [LayoutRenderer("username")]
    public class NLogUserName : LayoutRenderer
    {
        protected override void Append(StringBuilder builder, LogEventInfo logEvent)
        {
            builder.Append("wbs");
        }
    }

    /// <summary>
    /// 扩展NLog字段
    /// </summary>
    public static class NLogExtensions
    {
        /// <summary>
        /// 扩展用户信息
        /// </summary>
        /// <param name="builder"></param>
        /// <returns></returns>
        public static IWebHostBuilder NlogUser(this IWebHostBuilder builder)
        {
            LayoutRenderer.Register<NLogUserId>("userid");
            LayoutRenderer.Register<NLogUserName>("username");
            return builder;
        }
    }
}
